package com.cat.slidingWindow;

import java.util.Arrays;

/**
 * @author cat
 * @description https://leetcode.cn/problems/frequency-of-the-most-frequent-element/description/
 * @create 2025/7/30 10:40
 * @since JDK17
 */

public class Solution12 {
    void reverse(int[] nums) {
        for (int l = 0, r = nums.length - 1, t; l < r; l++, r--) {
            t = nums[l];
            nums[l] = nums[r];
            nums[r] = t;
        }
    }

    public int maxFrequency(int[] nums, int k) {
        int ans = 0, n = nums.length, sum = 0;
        Arrays.sort(nums);
        reverse(nums);  // 反转数组
        for (int l = 0, r = 0; r < n; l++) {
            // 扩大窗口
            while (r < n && sum > 0) {
                sum -= nums[l] - nums[r];
                r++;
            }
            // [l, r)
            ans = Math.max(ans, r - l);
            // 吐数字
            sum += (nums[l] - nums[l + 1]) * (r - l - 1);
        }
        return ans;
    }
}
